<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Sponsor_model extends CI_Model {
    
    function list_sponsor() {
        $query = $this->db->order_by('id','DESC')->get('sponsors');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return array();
    }
    
    function get_sponsor($sponsor_id) {
        $row = $this->db->where('id', $sponsor_id)->get('sponsors')->row_array();
        return $row ? $row : array();
    }
    
    function edit_sponsor($sponsor_id, $data_save)
    {
         $this->db->where('id',$sponsor_id)->update('sponsors',$data_save);
    }
    
    function list_sponsor_count_campaign() {
        $query = $this->db->select('sponsors.*,count(campaigns.id) as count_campaign')->join('campaigns','sponsors.id=campaigns.sponsor_id','LEFT')->group_by('sponsors.id')->order_by('sponsors.id','DESC')->get('sponsors');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return array();
    }
    
    function add_sponsor($data_save)
    {
        $this->db->insert('sponsors',$data_save);
    }
}